﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace StripConverter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int version = 5;
        int frames_count = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Multiselect = false;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (openFileDialog1.FileName != "")
                {
                    textBox1.Text = openFileDialog1.FileName;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (folderBrowserDialog1.SelectedPath != "")
                {
                    textBox2.Text = folderBrowserDialog1.SelectedPath;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Image i = Image.FromFile(textBox1.Text);
                Graphics g = Graphics.FromImage(i);
                int width = i.Width / (int)numericUpDown1.Value;
                int height = i.Height / (int)numericUpDown2.Value;
                int end = height;
                if (i.Height % 2 != 0)
                {
                    end = height + 1;
                    MessageBox.Show("Strip Animation is not Power of two.\nPress OK to Continue.", "Strip Converter", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                for (int k = 0; k < numericUpDown2.Value * height; k += end)
                {
                    for (int p = 0; p < numericUpDown1.Value * width; p += width)
                    {
                        Bitmap b1 = new Bitmap(i);
                        Bitmap b2 = new Bitmap(width, height);
                        for (int kk = 0; kk < width; kk++)
                        {
                            for (int pp = 0; pp < height; pp++)
                            {
                                b2.SetPixel(kk, pp, b1.GetPixel(p + kk, k + pp));
                            }
                        }
                        b2.Save(textBox2.Text + "\\frame_" + (k / height) + "-" + (p / width) + ".png");
                    }
                }
                MessageBox.Show("Converting Completed.", "Strip Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "Strip Converter", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button4.Enabled = false;
            button4.Text = "Please Wait...";
            button4.Refresh();
            try
            {
                System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://www.bluebulk.info/strip_converter/update.php");
                System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)req.GetResponse();
                System.IO.StreamReader st = new System.IO.StreamReader(resp.GetResponseStream());
                if (version < Convert.ToInt16(st.ReadLine()))
                {
                    if (MessageBox.Show("A Newer Version is Available. Click Ok To Download.", "Strip Converter", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        System.Diagnostics.Process.Start(st.ReadLine());
                    }
                }
                else
                {
                    MessageBox.Show("You Have The Final Version.", "Strip Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "Strip Converter", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            button4.Text = "Check for Updates";
            button4.Enabled = true;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            openFileDialog1.Multiselect = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                frames_count = 0;
                if (openFileDialog1.FileName != "")
                {
                    foreach (string file in openFileDialog1.FileNames)
                    {
                        frames_count++;
                    }
                    textBox4.Text = frames_count + " Frame(s) Loaded";
                }
                numericUpDown4.Value = frames_count;
                numericUpDown3.Value = 1;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (folderBrowserDialog1.SelectedPath != "")
                {
                    textBox3.Text = folderBrowserDialog1.SelectedPath;
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                Image i = Image.FromFile(openFileDialog1.FileNames[0]);
                Bitmap b = new Bitmap(i.Width * (int)numericUpDown4.Value, i.Height * (int)numericUpDown3.Value);
                int c = 0;
                int r = 0;
                foreach (string file in openFileDialog1.FileNames)
                {
                    Image i2 = Image.FromFile(file);
                    Bitmap b2 = new Bitmap(i2);
                    for (int j = 0; j < i.Width; j++)
                    {
                        for (int k = 0; k < i.Height; k++)
                        {
                            b.SetPixel(j + c, k + r, b2.GetPixel(j, k));
                        }
                    }
                    c += i.Width;
                    if (c >= i.Width * (int)numericUpDown4.Value)
                    {
                        c = 0;
                        if (r < i.Height * (int)numericUpDown3.Value)
                        {
                            r += i.Height;
                        }
                    }
                }
                b.Save(textBox3.Text + "\\strip.png");
                MessageBox.Show("Converting Completed.", "Strip Converter", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "Strip Converter", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "";
            folderBrowserDialog1.SelectedPath = "";
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            numericUpDown1.Value = 1;
            numericUpDown2.Value = 1;
            numericUpDown3.Value = 1;
            numericUpDown4.Value = 1;
        }

        int frame = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (frame)
            {
                case 0: pictureBox2.Image = StripConverter.Properties.Resources.frame_0_0; break;
                case 1: pictureBox2.Image = StripConverter.Properties.Resources.frame_1_0; break;
                case 2: pictureBox2.Image = StripConverter.Properties.Resources.frame_2_0; break;
                case 3: pictureBox2.Image = StripConverter.Properties.Resources.frame_3_0; break;
                case 4: pictureBox2.Image = StripConverter.Properties.Resources.frame_4_0; break;
                case 5: pictureBox2.Image = StripConverter.Properties.Resources.frame_5_0; break;
                case 6: pictureBox2.Image = StripConverter.Properties.Resources.frame_6_0; break;
                case 7: pictureBox2.Image = StripConverter.Properties.Resources.frame_7_0; break;
                case 8: pictureBox2.Image = StripConverter.Properties.Resources.frame_8_0; break;
                case 9: pictureBox2.Image = StripConverter.Properties.Resources.frame_9_0; break;
            }
            if (frame < 10)
            {
                frame++;
            }
            else
            {
                frame = 0;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.Bluebulk.info");
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            label11.Text = numericUpDown3.Value * numericUpDown4.Value + " Frame(s)";
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            label11.Text = numericUpDown3.Value * numericUpDown4.Value + " Frame(s)";
        }
    }
}
